<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl
 *  All Right Reserved
 ***********************/

namespace Modules\JasminumNews\Controllers;

use Gotcha\App\Framework\Controller;
use Gotcha\App\Types\Bool;
use Gotcha\App\Types\Int;
use GotchaDatabase\JasminumNewsLinksQuery;
use GotchaDatabase\JasminumNewsQuery;

class DefaultController extends Controller {
    public function IndexList() {
        return parent::View();
    }

    public function generateList() {
        // Local variables
        $paging = new Bool(false);

        $news = JasminumNewsQuery::create()
                    ->orderById('desc')
                            ->find();

        // No news?
        if ($news->count() === 0) {
            echo '<p>Nie ma dodanych newsów.</p>';
            return;
        }

        // Should we enable paging?
        if ($news->count() > 5) {
            $paging = new Bool(true);
            $news_amount = $news->count();
        }

        // Depending on existence of superglobal GET
        // containing current page we either select
        // top 5 news or exact ones
        if (!isset($_GET['page']) || empty($_GET['page'])) {
            $news = JasminumNewsQuery::create()
                        ->orderById('desc')
                            ->limit(5)
                                ->find();

            $_GET['page'] = new Int(0);
        } else {
            $_GET['page'] = new Int($_GET['page']);

            // We have to choose another limit of news
            $news = JasminumNewsQuery::create()
                        ->setOffset(($_GET['page']->int*5))
                            ->orderById('desc')
                                ->limit(5)
                                    ->find();
        }

        // HTML cache
        $html = '';
        $html .= '<div id="jasminum-news-wrapper">';

        foreach ($news as $single_news) {
            // Check if thumbnail exists
            $thumbnail = $single_news->getThumbnail();
            if (!empty($thumbnail)) {
                $thumbnail = '<div class="thumbnail"><img alt="'.$single_news->getTitle().'" src="'.GOTCHA_BASE_PATH.'/JasminumGallery/RenderPhoto?src=News/'.$thumbnail.'&width=220"></div>';
            } else {
                $thumbnail = '';
            }

            $html .= '<div id="news-'.$single_news->getId().'" class="single clearfix">';
            $html .= '<header>'.$single_news->getTitle().' '.$single_news->getCreationDate().'</header>';
            $html .= '<div class="news_body"><p>'.$thumbnail.$single_news->getBody().'</p></div>';

            // Check if there are links connected with this news
            $links = JasminumNewsLinksQuery::create()
                        ->filterByJasminumNewsId($single_news->getId())
                            ->find();

            if ($links->count() > 0) {
                $html .= '<div class="news_links">';
                $html .= '<p>Powiązane linki:</p>';
                $html .= '<ul>';

                // Display connected links
                foreach ($links as $link) {
                    $html .= '<li><a href="'.$link->getUrl().'">'.$link->getLink().'</a></li>';
                }

                $html .= '</ul></div>';
            }

            $html .= '</div>';
        }

        $html .= '</div>';

        // Paging
        if ($paging == true) {
            $html .= '<div id="jasminum-news-pager">';
            $html .= '<ul class="clearfix">';

            // Depending on news amount we create exact amount
            // of pages
            for ($_i = 0, $_length = $news_amount / 5; $_i <= $_length; $_i = $_i + 1) {
                if ($_GET['page'] == new Int($_i)) {
                    $html .= '<li>'.$_i.'</li>';
                } else {
                    $html .= '<li><a title="Strona '.$_i.'" href="'.GOTCHA_BASE_PATH.'/aktualnosci?page='.$_i.'">'.$_i.'</a></li>';
                }
            }

            $html .= '</ul></div>';
        }
        echo $html;
        return;
    }

    public function generateHomepageView() {
        $news = JasminumNewsQuery::create()
                    ->orderById('desc')
                        ->limit(2)
                            ->find();

        if ($news->count() === 0) {
            echo '<p>Nie ma dodanych newsów.</p>';
            return;
        }

        // HTML cache
        $html = '';
        $html .= '<div id="jasminum-news-wrapper">';

        foreach ($news as $single_news) {
            $html .= '<div id="news-'.$single_news->getId().'">';
            $html .= '<header><a href="'.GOTCHA_BASE_PATH.'/aktualnosci" title="Czytaj więcej...">'.$single_news->getTitle().' '.$single_news->getCreationDate().'</a></header>';

            // We have to make full news body shorter
            $body_words = explode(' ', $single_news->getBody());
            if(count($body_words) > 40) {
                $body = '';

                // Iterate through array, implode given amount
                // of words
                for ($_i = 0, $_length = 39; $_i <= $_length; $_i = $_i + 1) {
                    $body .= $body_words[$_i].' ';
                }

                $body .= '...';
            } else {
                $body = $single_news->getBody();
            }
            $html .= '<div class="news_body">'.$body.'<div class="news-more pull-right"><a href="'.parent::UrlContent('/aktualnosci').'">Więcej</a></div></div>';
            $html .= '</div>';
        }

        $html .= '</div>';
        echo $html;
        return;
    }
}